package com.itheima.health.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Api(tags = "检查项管理开发")
//@PreAuthorize("hasRole('')")    权限颗粒度 粗
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;  //  gui

    @GetMapping("checkitem/findAll")
    @ApiOperation(value = "查询功能",notes = "查询所有的检查项列表信息")
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")  // 颗粒度比较细
    public Result findAll(HttpServletResponse response){
        return new Result(checkItemService.list());
    }
    @PostMapping("checkitem/findPage")
    @ApiOperation(value = "分页查询功能",notes = "查询所有的检查项列表信息")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        //    分页查询  条件  页码  记录数  条件
        //  集合 插件  PageResult对象
        return new Result(checkItemService.findPage(queryPageBean));
    }

    @DeleteMapping("checkitem/deleteCheckItemById/{id}")
    @ApiOperation(value = "分页查询功能",notes = "查询所有的检查项列表信息")
    public Result deleteCheckItemById(@PathVariable("id") int id){
        CheckItem checkItem = new CheckItem();
        checkItem.setId(id);
        checkItem.setIs_delete(1);  //  update .... set  is_delete = 1 where id = ?
         return  new Result(checkItemService.updateById(checkItem));
    }

    @PostMapping("checkitem/saveOrUpdate")
    @ApiOperation(value = "添加功能",notes = "添加检查项")
    public Result  add(@RequestBody CheckItem checkItem ,HttpServletResponse response){


        if(checkItem.getId()==null){
            checkItem.setIs_delete(0);  //  如果添加操作 初始化 is_delete =0
        }
        //  saveOrUpdate   添加和修改功能   判断  checkitem 有无 主键id   没有id  save    存在主键id  update
        return  new Result(checkItemService.saveOrUpdate(checkItem));

    }


    @PostMapping("checkitem/deletedata")
    public Result findalldeletedta(@RequestBody QueryPageBean queryPageBean){
        PageResult findalldelete = checkItemService.findalldelete(queryPageBean);
        return new Result(findalldelete);
    }

    @GetMapping("checkitem/redata/{id}")
    public Result redata(@PathVariable("id") Integer id){
        CheckItem checkItem = new CheckItem();
        checkItem.setIs_delete(0);
        checkItem.setId(id);
        return new Result(checkItemService.updateById(checkItem));
    }
}
